package com.sanqing.iframe;

import com.sanqing.model.*;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;

import com.sanqing.dao.Dao;

public class RevokeReserveFrame extends JInternalFrame{

	private int getSelectTableRow=-1;
	final JScrollPane scrollPane;
	private JTable table;
    private Object[][] results;
    private Object[][] Results;
    private String[] columnNames;
	private Object[][] getFileStates(List list) {
        // 新增：在列名中添加"语言"
        String[] columnNames = { "图书编号", "图书类别", "图书名称", "作者", "译者", "出版商", "语言", "出版日期", "价格", "库存数量" };
        Object[][] results = new Object[list.size()][columnNames.length];
        for (int i = 0; i < list.size(); i++) {
            BookInfo bookinfo = (BookInfo) list.get(i);
            results[i][0] = bookinfo.getISBN();
            Integer typeId = bookinfo.getTypeid();
            String bookTypeName = MapPz.getIdToName().get(typeId);
            results[i][1] = bookTypeName;
            results[i][2] = bookinfo.getBookname();
            results[i][3] = bookinfo.getWriter();
            results[i][4] = bookinfo.getTranslator();
            results[i][5] = bookinfo.getPublisher();
            results[i][6] = bookinfo.getLanguage(); // 新增：获取语言信息
            results[i][7] = bookinfo.getDate();
            results[i][8] = bookinfo.getPrice();
            results[i][9] = bookinfo.getNumber(); // 添加库存数量到表格
        }
        return results;
    }
	public RevokeReserveFrame()
	{
		super();
        final BorderLayout borderLayout = new BorderLayout();
        getContentPane().setLayout(borderLayout);
        setIconifiable(true);
        setClosable(true);
        setTitle("我的预约");
        setBounds(20, 100, 850, 406); // 新增：调整窗体宽度以容纳新控件
        final JPanel mainPanel = new JPanel();
        final BorderLayout borderLayout_1 = new BorderLayout();
        borderLayout_1.setVgap(5);
        mainPanel.setLayout(borderLayout_1);
        mainPanel.setBorder(new EmptyBorder(5, 10, 5, 10));
        getContentPane().add(mainPanel);
        scrollPane = new JScrollPane();
        mainPanel.add(scrollPane);
        
        final JPanel bookPanel = new JPanel();
        mainPanel.add(bookPanel, BorderLayout.SOUTH);
        final GridLayout gridLayout = new GridLayout(1, 2);
        bookPanel.setLayout(gridLayout);
        final JButton revokeReserve=new JButton();
        final JButton closeRevokeReserve=new JButton();
        revokeReserve.setText("取消预约");
        closeRevokeReserve.setText("关闭");
        revokeReserve.addActionListener(new revokeReserveActin());
        bookPanel.add(revokeReserve);
        closeRevokeReserve.addActionListener(new closeButtonAction());
        bookPanel.add(closeRevokeReserve);
        //int a=Dao.selectReserve("select * from tb_reserve where readerISBN='"+BookLoginIFrame.getOperator().getISBN()+"'").size();
        //System.out.println(a);
        Results=getFileStates(Dao.selectBookinfo("select * from tb_bookinfo where ISBN IN(select bookISBN from tb_reserve where readerISBN='"+BookLoginIFrame.getOperator().getISBN()+"')"));
        tableR();
	}
	private void tableR()
    {
		columnNames =new String[] {"图书编号", "图书类别", "图书名称", "作者", "译者", "出版商", "出版日期", "价格","图书数量"}; // 正确声明
	    table = new JTable(Results, columnNames);
	    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
	    scrollPane.setViewportView(table);
	    table.addMouseListener(new TableListener());
    }
	private class TableListener implements MouseListener{

		@Override
		public void mouseClicked(MouseEvent arg0) {
			// TODO Auto-generated method stub
			getSelectTableRow=table.getSelectedRow();
		}

		@Override
		public void mouseEntered(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseExited(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mousePressed(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseReleased(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		
		
	}
	private class closeButtonAction implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			doDefaultCloseAction();//关闭窗体
		}
		
	}
	private class revokeReserveActin implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			if(getSelectTableRow!=-1)
			{
				DateTimeFormatter dateTimeFormatter1=DateTimeFormatter.ofPattern("yyyy-MM-dd");
		    	String borrowDate1=dateTimeFormatter1.format(LocalDate.now());
		    	//String borrowDate=dateTimeFormatter1.format(LocalDate.now().minusDays(7));
		    	ArrayList list=(ArrayList) Dao.selectReserve("select * from tb_reserve where readerISBN='"+BookLoginIFrame.getOperator().getISBN()+"'and bookISBN='"+Results[getSelectTableRow][0]+"'");
		    	for(int i=0;i<list.size();i++)
		    	{
		    		Reserve reserve=(Reserve)list.get(i);
		    		//System.out.println(reserve.getreaderISBN());
		    		
		    		Dao.InsertBorrow("delete from tb_reserve where readerISBN='"+reserve.getreaderISBN()+"' and bookISBN='"+reserve.getbookISBN()+"'");
		    		if(!Objects.isNull(reserve.getreserveDate()))
		    		{
		    		String se=Dao.selectReserveReaderISBN("select * from tb_reserve where bookISBN='"+reserve.getbookISBN()+"' and reserveDate IS NULL LIMIT 1");
		    		//System.out.println(se);
		    		Dao.InsertBorrow("update tb_reserve set reserveDate = '"+borrowDate1+"' where readerISBN = '"+se+"'and bookISBN='"+reserve.getbookISBN()+"'");
		    		}
		    	}
		    	JOptionPane.showMessageDialog(null,"成功取消约过图书","提示",JOptionPane.ERROR_MESSAGE);
				doDefaultCloseAction();//关闭窗体
			}
			else
			{
				DateTimeFormatter dateTimeFormatter1=DateTimeFormatter.ofPattern("yyyy-MM-dd");
		    	String borrowDate1=dateTimeFormatter1.format(LocalDate.now());
		    	//String borrowDate=dateTimeFormatter1.format(LocalDate.now().minusDays(7));
		    	ArrayList list=(ArrayList) Dao.selectReserve("select * from tb_reserve where readerISBN='"+BookLoginIFrame.getOperator().getISBN()+"'");
		    	for(int i=0;i<list.size();i++)
		    	{
		    		Reserve reserve=(Reserve)list.get(i);
		    		//System.out.println(reserve.getreaderISBN());
		    		
		    		Dao.InsertBorrow("delete from tb_reserve where readerISBN='"+reserve.getreaderISBN()+"' and bookISBN='"+reserve.getbookISBN()+"'");
		    		if(!Objects.isNull(reserve.getreserveDate()))
		    		{
		    		String se=Dao.selectReserveReaderISBN("select * from tb_reserve where bookISBN='"+reserve.getbookISBN()+"' and reserveDate IS NULL LIMIT 1");
		    		//System.out.println(se);
		    		Dao.InsertBorrow("update tb_reserve set reserveDate = '"+borrowDate1+"' where readerISBN = '"+se+"'and bookISBN='"+reserve.getbookISBN()+"'");
		    		}
		    	}
		   		JOptionPane.showMessageDialog(null,"全部成功取消约过图书","提示",JOptionPane.ERROR_MESSAGE);
				doDefaultCloseAction();//关闭窗体
			}
		}
		
	}

}